var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { getConfigPath } from '@mintlify/prebuild';
import detect from 'detect-port';
import Ora from 'ora';
import { CMD_EXEC_PATH } from './constants.js';
export const checkPort = (argv) => __awaiter(void 0, void 0, void 0, function* () {
    const initialPort = typeof argv.port === 'number' ? argv.port : 3000;
    if (initialPort === (yield detect(initialPort)))
        return initialPort;
    for (let port = initialPort + 1; port < initialPort + 10; port++) {
        console.log(`Port ${port - 1} is already in use. Trying ${port} instead.`);
        if (port === (yield detect(port)))
            return port;
    }
});
export const checkNodeVersion = () => __awaiter(void 0, void 0, void 0, function* () {
    let nodeVersionString = process.version;
    if (nodeVersionString.charAt(0) === 'v') {
        nodeVersionString = nodeVersionString.slice(1);
    }
    const versionArr = nodeVersionString.split('.');
    const majorVersion = parseInt(versionArr[0], 10);
    if (majorVersion < 18) {
        const logger = buildLogger();
        logger.fail(`Mintlify requires a node version >= 18.0.0 (current version ${nodeVersionString}). Try removing the mintlify package, upgrading node, reinstalling mintlify, and running again.`);
        process.exit(1);
    }
});
export const buildLogger = (startText = '') => {
    const logger = Ora().start(startText);
    return logger;
};
export const checkForMintJson = () => __awaiter(void 0, void 0, void 0, function* () {
    if (yield getConfigPath(CMD_EXEC_PATH))
        return;
    console.error('Must be run in a directory where a mint.json file exists.');
    process.exit(1);
});
